<template>
    <div class="header-user">
        <nuxt-link to="/user">
            <em v-text="getUserName()" :style="'background-color:' + randomColor()"></em>
        </nuxt-link>
        <cite>
            <CommonLevelDiscount :level="userInfo.level" :other="{email:userInfo.email}" type="icon|email" />
            <nuxt-link to="/user">Dashboard</nuxt-link>
            <nuxt-link to="/">Home</nuxt-link>
            <a v-if="loginOut" @click.prevent="doLoginout()">Log out</a>
        </cite>
    </div>
</template>
<script>
import { mapGetters, mapState } from "vuex";
export default {
    name: "HeaderbarCurrency",
    data () {
        return {
            loginOut: true,
        };
    },
    computed: {
        ...mapGetters({
            isLogin: "getIsLogin",
        }),
        ...mapGetters("user", {
            userInfo: "getUserInfo",
        }),
    },
    methods: {
        // 用户名
        getUserName () {
            if (this.userInfo && this.userInfo.name) {
                let str = String(this.userInfo.name);
                let first = str[0] || "N";
                return first.toUpperCase();
            } else {
                return "";
            }
        },
        randomColor () {
            const color = {
                1: '#23B98F',
                2: '#C5A886',
                3: '#A0AFBD',
                4: '#DBAA36',
                5: '#3EABD1',
                6: '#EA4C89',
                7: '#9878D1',
            }
            const level = Math.max(Math.min((this.userInfo || {}).level, 7), 1);
            return color[level];
        },
        doLoginout () {
            this.$emit("global:beforeLogout").then(() => this.$store.dispatch('doLogout')).then(() => this.$emit("global:afterLogout")).catch(err => { })
        },
    },
    mounted () {
        if (window.top == window.self) {
            this.loginOut = true;
        } else {
            this.loginOut = false;
        }
    },

};
</script>
<style lang="scss" scoped>
.header-user {
    position: relative;
    width: 28px;
    min-width: 28px;
    margin-left: 12px;
    display: flex;
    align-items: center;
    justify-content: center;

    & > a {
        display: flex;
        align-items: center;
        justify-content: center;
        position: relative;
        border-radius: 28px;
        cursor: pointer;
        overflow: hidden;
        width: 28px;
        height: 28px;
        background-color: transparent;
        transition: all 0.2s;

        & > em {
            width: 22px;
            height: 22px;
            border-radius: 22px;
            font-size: 14px;
            line-height: initial;
            text-align: center;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 14px;
            // font-family: Roboto-Regular, Roboto;
            font-weight: 400;
            color: #ffffff;
        }

        &:hover {
            background-color: rgba(0, 0, 0, 0.06);
        }
    }

    & > em {
        display: none;
    }

    cite {
        position: absolute;
        top: 100%;
        right: 0;
        transform: translateY(4px);
        min-width: 200px;
        padding: 0 0 12px;
        line-height: 30px;
        background: #ffffff;
        box-shadow: 0px 4px 8px 0px rgba(0, 0, 0, 0.12);
        border-radius: 6px;
        border: 1px solid #d9d9d9;
        opacity: 0;
        pointer-events: none;
        transition: all 0.2s;
        &::before {
            content: '';
            display: block;
            width: 200px;
            height: 8px;
            position: absolute;
            bottom: 100%;
            left: 50%;
            transform: translateX(-50%);
        }
        .level-discount {
            cursor: default;
            border-radius: 5px 5px 0 0;
            display: flex;
            flex-direction: row;
            align-items: center;
            justify-content: center;
            box-sizing: border-box;
            padding: 12px;
            background-color: #f6f6f6;
            width: 100%;
            height: 56px;
            white-space: nowrap;
            font-size: 14px;
            font-weight: 400;
            line-height: 32px;
            margin-bottom: 10px;

            &::v-deep {
                span[name='icon'] {
                    width: 16px;
                    height: 16px;
                    background-size: 16px 16px;
                }
            }
            p {
                font-size: 14px;
                white-space: nowrap;
                text-align: center;
                display: -webkit-box;
                -webkit-box-orient: vertical;
                -webkit-line-clamp: 2;
                overflow: hidden;
                margin-left: 4px;
            }
        }

        a {
            display: block;
            line-height: 30px;
            white-space: nowrap;
            text-align: center;
            font-size: 14px;
            color: #555;
            cursor: pointer;
            &:nth-child(3) {
                display: none;
            }
            &:not(:last-child):hover {
                color: #ea4c89;
            }
            &:last-child:hover {
                color: #f44336;
            }
        }
    }

    &:hover cite {
        opacity: 1;
        pointer-events: all;
    }
}

@media (max-width: 960px) {
    .header-user {
        position: absolute;
        top: 50%;
        transform: translateY(-50%);
        right: 14px;
        min-width: 26px;
        width: 26px;

        & > em {
            display: block;
            width: 26px;
            height: 26px;
            line-height: 26px;
            font-size: 14px;
            font-weight: bold;
        }

        & > cite {
            left: auto;
            right: 0;
            transform: translate(0, 4px);

            strong {
                padding: 15px 12px;
                line-height: 18px;
            }
            a {
                height: 52px;
                line-height: 52px;
                font-size: 16px;
            }
        }
    }
}
</style>
